package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"xueyuan_backend/service"
)

// CollectRecordHandle 用户收藏文章记录
func CollectRecordHandle(c *gin.Context) {
	user_id := c.Query("user_id")
	articles := service.CollectRecord(user_id)
	//if len(articles) != 0 {
	c.JSON(http.StatusOK, &articles)
	//}
}

// AddUserHandle 添加用户，根据code存储返回openid
func AddUserHandle(c *gin.Context) {
	var code string
	var exist bool
	code, exist = c.GetPostForm("code")
	if exist {
		openid := service.AddUser(code)
		c.JSON(http.StatusOK, openid)
	}
}

// UserProfHandle 添加用户专业及默认订阅
func UserProfHandle(c *gin.Context) {
	var prof string
	var user_id string
	var exist bool
	user_id, exist = c.GetPostForm("user_id")
	prof, exist = c.GetPostForm("prof")
	if exist {
		service.UserProf(user_id, prof)
	}
}

// SearchWebsiteHandle 关键字搜索网站
func SearchWebsiteHandle(c *gin.Context) {
	keyword := c.Query("keyword")
	websites := service.SearchWebsite(keyword)
	c.JSON(http.StatusOK, websites)
}

// AddSubscribeHandle 添加订阅网站
func AddSubscribeHandle(c *gin.Context) {
	var user_id string
	var website_url string
	//TODO
	var exist bool
	user_id, exist = c.GetPostForm("user_id")
	website_url, exist = c.GetPostForm("website_url")
	if exist {
		service.AddSubscribe(user_id, website_url)
	}
}

// DeleteSubscribeHandle 删除订阅网站
func DeleteSubscribeHandle(c *gin.Context) {
	var user_id string
	var website_url string
	//TODO
	var exist bool
	user_id, exist = c.GetPostForm("user_id")
	website_url, exist = c.GetPostForm("website_url")
	if exist {
		service.DeleteSubscribe(user_id, website_url)
	}
}

// SubscribeViewHandle 返回该用户订阅网站
func SubscribeViewHandle(c *gin.Context) {
	user_id := c.Query("user_id")
	websites := service.SubscribeView(user_id)
	c.JSON(http.StatusOK, websites)
}

// SearchArticleHandle 关键字搜索文章标题
func SearchArticleHandle(c *gin.Context) {
	keyword := c.Query("keyword")
	user_id := c.Query("user_id")
	page, _ := strconv.Atoi(c.Query("page"))
	articles := service.SearchArticle(keyword, user_id, page)
	c.JSON(http.StatusOK, articles)
}

// SearchCollectArticleHandle 关键字搜索收藏文章标题
func SearchCollectArticleHandle(c *gin.Context) {
	keyword := c.Query("keyword")
	user_id := c.Query("user_id")
	articles := service.SearchCollectArticle(keyword, user_id)
	c.JSON(http.StatusOK, articles)
}

// ScanArticleHandle 分栏目浏览文章
func ScanArticleHandle(c *gin.Context) {
	var user_id string
	var web_category string
	//TODO
	user_id = c.Query("user_id")
	web_category = c.Query("web_category")
	page, _ := strconv.Atoi(c.Query("page"))
	articles := service.ScanArticle(user_id, web_category, page)
	c.JSON(http.StatusOK, articles)
}

// ViewArticleHandle 查看文章详情
func ViewArticleHandle(c *gin.Context) {
	var article_id string
	article_id = c.Query("article_id")
	article := service.ViewArticle(article_id)
	c.JSON(http.StatusOK, article)
}

// AddCollectHandle 添加用户收藏文章
func AddCollectHandle(c *gin.Context) {
	var user_id string
	var article_id string
	//TODO
	var exist bool
	user_id, exist = c.GetPostForm("user_id")
	article_id, exist = c.GetPostForm("article_id")
	if exist {
		service.AddCollect(user_id, article_id)
	}
}

// DeleteCollectHandle 删除用户收藏文章
func DeleteCollectHandle(c *gin.Context) {
	var user_id string
	var article_id string
	//TODO
	var exist bool
	user_id, exist = c.GetPostForm("user_id")
	article_id, exist = c.GetPostForm("article_id")
	if exist {
		service.DeleteCollect(user_id, article_id)
	}
}
